package com.wlscale.roadsystem.wiget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.wlscale.roadsystem.R;

import java.util.Date;


/**
 * 描述: {选择开始结束时间弹出框}
 * 作者: jack-song
 * 创建日期: 2017/11/7 10:04
 * 修改日期: 2017/11/7
 * 版本: {版本}
 */
public class ChoiceTimeAlertDialog extends Dialog implements View.OnClickListener {
    /**
     * 上下文
     */
    private Context context;
    /**
     * 提示
     */
    private TextView tvTitle;
    /**
     *
     */
    private TextView tvMessage;
    /**
     * 开始时间
     */
    private TextView tv_start_date_picker;
    /**
     * 结束时间
     */
    private TextView tv_end_date_picker;
    /**
     * 开始时间按钮
     */
    private LinearLayout btn_start_date_picker;
    /**
     * 结束时间按钮
     */
    private LinearLayout btn_end_date_picker;
    /**
     * 左侧按钮
     */
    private Button btnLeft;
    /**
     * 右侧按钮
     */
    private Button btnRigth;
    /**
     * 提示
     */
    private CharSequence title;
    /**
     *
     */
    private CharSequence message;
    /**
     * 左侧文字
     */
    private CharSequence leftText;
    /**
     * 右侧文字
     */
    private CharSequence rigthText;
    /**
     * 开始时间
     */
    private String startTime;
    /**
     * 结束时间
     */
    private String endTime;
    /**
     * 按钮
     */
    private OnClickLister onClickLister;

    public ChoiceTimeAlertDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public ChoiceTimeAlertDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_chose_time);
        tvTitle = findViewById(R.id.tv_title);
        tvMessage = findViewById(R.id.tv_msg);
        tv_start_date_picker = findViewById(R.id.tv_start_date_picker);
        tv_end_date_picker = findViewById(R.id.tv_end_date_picker);
        btn_start_date_picker = findViewById(R.id.btn_start_date_picker);
        btn_end_date_picker = findViewById(R.id.btn_end_date_picker);
        btnLeft = findViewById(R.id.btn_left);
        btnRigth = findViewById(R.id.btn_rigth);
        btn_start_date_picker.setOnClickListener(this);
        btn_end_date_picker.setOnClickListener(this);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
            tvTitle.setText("");
        } else {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        if (!TextUtils.isEmpty(message)) {
            tvMessage.setText(message);
        } else {
            tvMessage.setText("");
        }
            btnLeft.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(leftText)) {
                btnLeft.setText(leftText);
            }
            btnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
            btnRigth.setVisibility(View.GONE);
            btnRigth.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(rigthText)) {
                btnRigth.setText(rigthText);
            }
            btnRigth.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != onClickLister){
                        onClickLister.onClickLister(startTime,endTime);
                    }
                    dismiss();
                }
            });
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager wm = window.getWindowManager();
        Point windowSize = new Point();
        wm.getDefaultDisplay().getSize(windowSize);
        window.getAttributes().width = (int) (windowSize.x * 0.8f);
        window.getAttributes().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);

    }

    public void setTitle(CharSequence title) {
        this.title = title;
    }

    public void setMessage(CharSequence message) {
        this.message = message;
    }

    public void setLeftText(CharSequence leftText) {
        this.leftText = leftText;
    }

    public void setRigthText(CharSequence rigthText) {
        this.rigthText = rigthText;
    }

    public void setOnClickLister(OnClickLister onClickLister) {
        this.onClickLister = onClickLister;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_start_date_picker:
                choiceTime(true);
                break;
            case R.id.btn_end_date_picker:
                choiceTime(false);
                break;
        }
    }

    public static class Builder {
        /**
         * 上下文
         */
        private Context context;
        /**
         * 提示
         */
        private CharSequence title;
        /**
         * 提示
         */
        private CharSequence message;
        /**
         * 左侧文字
         */
        private CharSequence leftText;
        /**
         * 右侧文字
         */
        private CharSequence rigthText;
        /**
         * 按钮
         */
        private OnClickLister onClickLister;

        public Builder() {
        }

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setTitle(CharSequence title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(CharSequence message) {
            this.message = message;
            return this;
        }


        public Builder setLeftText(CharSequence leftText) {
            this.leftText = leftText;
            return this;
        }

        public Builder setRigthText(CharSequence rigthText) {
            this.rigthText = rigthText;
            return this;
        }
        public Builder setOnClickLister(OnClickLister onClickLister) {
            this.onClickLister = onClickLister;
            return this;
        }


        public ChoiceTimeAlertDialog builder() {
            ChoiceTimeAlertDialog loadingDialog = new ChoiceTimeAlertDialog(context, R.style.loading_dialog);
            loadingDialog.setTitle(title);
            loadingDialog.setMessage(message);
            loadingDialog.setLeftText(leftText);
            loadingDialog.setRigthText(rigthText);
            loadingDialog.setOnClickLister(onClickLister);
            return loadingDialog;
        }
    }

    public interface OnClickLister{
        void onClickLister(String startTime,String endTime);
    }


    /**
     * 选择时间
     * @param isStart
     */
    private void choiceTime(boolean isStart){
        TimePickerView pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {

            }
        }).setType(new boolean[]{true, true, true,false,false,false})//分别对应年月日时分秒，默认全部显示
                .isDialog(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 50;
            params.rightMargin = 50;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(R.style.loading_dialog);//修改动画样式
                dialogWindow.setGravity(Gravity.CENTER);//改成Bottom,底部显示
            }
        }
        pvTime.show();
    }
}
